package com.eliza.base.views.learn.text

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController

object TextStyleX {
    @Composable
    fun Show(navController: NavController) {
        Column {
            var count by remember {
                mutableIntStateOf(0)
            }
            Button(onClick = { count = (count + 1) % 3}) {
                Text(text = "PointerX $count")
            }
            when (count) {
                0 -> TextShadow()
                1 -> TextFieldX()
                2 -> MultipleStylesInText()
            }
        }

    }

    @Composable
    private fun TextShadow() {
        val offset = Offset(5.0f, 10.0f)
        val textShadow: @Composable (shadow: Shadow) -> Unit = { shadow ->
            Text(
                text = "Hello world!",
                style = TextStyle(
                    fontSize = 24.sp,
                    shadow = shadow
                )
            )
        }
        Column {
            textShadow(
                Shadow(
                    color = Color.Blue,
                    offset = offset,
                    blurRadius = 3f
                )
            )
            textShadow(
                Shadow(
                    color = Color.Blue,
                    offset = offset,
                    blurRadius = 10f
                )
            )
            textShadow(
                Shadow(
                    color = Color.Blue,
                    offset = Offset(10.0f, 10.0f),
                    blurRadius = 10f
                )
            )
        }
    }

    @Composable
    private fun TextFieldX() {
        val rainbowColors = listOf(
            Color.Black,
            Color.DarkGray,
            Color.Gray,
            Color.LightGray,
            Color.White,
            Color.Red,
            Color.Green,
            Color.Blue,
            Color.Yellow,
            Color.Cyan,
            Color.Magenta,
            Color.Transparent,
        )
        var text by remember { mutableStateOf("") }
        val brush = remember {
            Brush.linearGradient(
                colors = rainbowColors
            )
        }
        TextField(
            value = text, onValueChange = { text = it }, textStyle = TextStyle(brush = brush)
        )
    }

    @Composable
    private fun MultipleStylesInText() {
        Column {
            Text(text = "buildAnnotatedString")
            Text(
                buildAnnotatedString {
                    withStyle(style = SpanStyle(color = Color.Blue)) {
                        append("H")
                    }
                    append("ello ")
                    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Red)) {
                        append("W")
                    }
                    append("orld")
                })
            AlignedText()
        }
    }

    @Composable
    private fun AlignedText() {
        val textLineHeightStyle: @Composable (color: Color, lineHeightStyle: LineHeightStyle) -> Unit =
            { color, lineHeightStyle ->
                Text(
                    modifier = Modifier.background(color),
                    text = "hello world",
                    style = LocalTextStyle.current.merge(
                        TextStyle(
                            lineHeight = 2.em,
                            platformStyle = PlatformTextStyle(
//                    includeFontPadding 是一个旧版属性，
//                    PlatformTextStyle 将 includeFontPadding 设置为 false（这将移除多余的内边距），然后进一步调整文字。
                                includeFontPadding = false
                            ),
                            lineHeightStyle = lineHeightStyle
                        )
                    )
                )

            }
        Column {
            textLineHeightStyle(
                Color.Cyan,
                LineHeightStyle(
                    alignment = LineHeightStyle.Alignment.Center,
                    trim = LineHeightStyle.Trim.None
                )
            )
            textLineHeightStyle(
                Color.Gray,
                LineHeightStyle(
                    alignment = LineHeightStyle.Alignment.Center,
                    trim = LineHeightStyle.Trim.None
                )
            )
        }
    }

}